home *** CD-ROM | disk | FTP | other *** search
- unit DrBobCGI;
- interface
- type
- TRequestMethod = (Unknown,Get,Post);
- var
- RequestMethod: TRequestMethod = Unknown;
-
- var
- ContentLength: Integer = 0;
-
- function Value(const Field: ShortString): ShortString;
- function CookieValue(const Field: ShortString): ShortString;
-
- implementation
- uses
- Windows, SysUtils;
-
- function _Value(const Field: ShortString;
- const Data: AnsiString; Sep: Char = '&'): ShortString;
- { 1998/01/02: check for complete match of Field name }
- { 1999/03/01: do conversion *after* searching fields }
- var
- i: Integer;
- Str: String[3];
- len: Byte absolute Result;
- begin
- len := 0; { Result := '' }
- i := Pos('&'+Field+'=',Data);
- if i = 0 then
- begin
- i := Pos(Field+'=',Data);
- if i > 1 then i := 0
- end
- else Inc(i); { skip '&' }
- if i > 0 then
- begin
- Inc(i,Length(Field)+1);
- while Data[i] <> Sep do
- begin
- Inc(len);
- if Data[i] = '%' then // special code
- begin
- Str := '$00';
- Str[2] := Data[i+1];
- Str[3] := Data[i+2];
- Inc(i,2);
- Result[len] := Chr(StrToInt(Str))
- end
- else Result[len] := Data[i];
- Inc(i)
- end
- end
- end {_Value};
-
- const
- Data: AnsiString = '';
-
- function Value(const Field: ShortString): ShortString;
- begin
- Result := _Value(Field, Data)
- end;
-
- const
- Cookie: AnsiString = '';
-
- function CookieValue(const Field: ShortString): ShortString;
- begin
- Result := _Value(Field, Cookie, ';');
- if Result = '' then Result := Cookie { debug }
- end;
-
- var
- P: PChar;
- i: Integer;
- Str: ShortString;
-
- initialization
- P := GetEnvironmentStrings;
- while P^ <> #0 do
- begin
- Str := StrPas(P);
- if Pos('REQUEST_METHOD=',Str) > 0 then
- begin
- Delete(Str,1,Pos('=',Str));
- if Str = 'POST' then RequestMethod := Post
- else
- if Str = 'GET' then RequestMethod := Get
- end;
- if Pos('CONTENT_LENGTH=',Str) = 1 then
- begin
- Delete(Str,1,Pos('=',Str));
- ContentLength := StrToInt(Str)
- end;
- if Pos('QUERY_STRING=',Str) > 0 then
- begin
- Delete(Str,1,Pos('=',Str));
- SetLength(Data,Length(Str)+1);
- Data := Str
- end;
- if Pos('HTTP_COOKIE=',Str) > 0 then
- begin
- Delete(Str,1,Pos('=',Str));
- SetLength(Cookie,Length(Str)+1);
- Cookie := Str
- end;
- Inc(P, StrLen(P)+1)
- end;
- if RequestMethod = Post then
- begin
- SetLength(Data,ContentLength+1);
- for i:=1 to ContentLength do read(Data[i]);
- Data[ContentLength+1] := '&';
- { if IOResult <> 0 then { skip }
- end;
- i := 0;
- while i < Length(Data) do
- begin
- Inc(i);
- if Data[i] = '+' then Data[i] := ' ';
- { if Data[i] = '%' then // special code
- begin
- Str := '$00';
- Str[2] := Data[i+1];
- Str[3] := Data[i+2];
- Delete(Data,i+1,2);
- Data[i] := Chr(StrToInt(Str))
- end }
- end;
- if i > 0 then Data[i+1] := '&'
- else Data := '&';
- finalization
- Data := ''
- end.
-